////////////////////////////////////////////////////////////////////////////////
//
//	This file is part of Woozox, see <http://code.google.com/p/woozox/>.
//
//	Copyright 2008-2009 Florian David
//
//	Woozoo is free software: you can redistribute it and/or modify
//	it under the terms of the GNU Lesser General Public License as 
//	published by the Free Software Foundation, either version 3 of 
//	the License, or (at your option) any later version.
//
//	Woozoo is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU Lesser General Public 
//	License along with Woozoo. If not, see <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
package org.woozoo.xhtml.core.util
{
	import org.woozoo.xhtml.core.base.Element;
	
	/**
	 * @private 
	 * */
	public class ElementArrayUtil
	{
		/**
		 * 
		 * */
		public static function getAttribute(list : Array, attributeName : String) : Array {
			if (!list) 
				return [];
			var res : Array = []
			var len : int = list.length;
			for (var i : int = 0; i < len; i++) {
				var element : Element = list[i];
				res.push(element.att(attributeName));
			}
			return res;	
		}
		
		/**
		 * 
		 * */
		public static function getAttributes(list : Array, args : Array) : Array {
			if (!list) 
				return [];
			var argsLen : int = args.length;	
				
			var res : Array = []
			var len : int = list.length;
			for (var i : int = 0; i < len; i++) {
				var element : Element = list[i];
				var obj : Object = {};
				for (var j : int = 0; j < argsLen; j++) {
					var arg : String = args[j];
					obj[arg] = element.att(arg);
				}
				res.push(obj);
			}
			return res;	
		}
		
		/**
		 * 
		 * */
		public static function mergeUnique(list1 : Array, list2 : Array) : Array {
			var len : int = list2.length - 1;
			for (var i : int = list1.length - 1; i >= 0; i--) {
				var element1 : Element = list1[i];
				for (var j : int = len; j >= 0 ; j--) {
					if (element1 == list2[j]) {
						list1.splice(i, 1);
						break;
					}
				}
			}
			return list1.concat(list2);
		}
		
		/**
		 * 
		 * */
		public static function groupByParent(list : Array) : Object {
			var res : Object = {};
			var len : int = list.length;
			for (var i : int = 0; i < len; i++) {
				var element : Element = list[i];
				var parentUID : String = element.parent.wzxInternalUID;
				if (!res[parentUID]) {
					res[parentUID] = [];
				}
				(res[parentUID] as Array).push(element);
			}
			return res;
		}
		
		public static function isEqual(list1 : Array, list2 : Array, orderStrict : Boolean = true) : Boolean {
			if (!list1 && !list2) return true;
			if (!list1 && list2) return false;
			if (!list2 && list1) return false;
			
			var len1 : int = list1.length;
			var len2 : int = list2.length;
			if (len1 != len2) return false;
			
			var i : int
			if (orderStrict) {
				for (i = 0; i < len1; i++) {
					if (list1[i] != list2[i]) {
						return false;
					}
				}
			} else {
				var count : int = 0;
				for (i = 0; i < len1; i++) {
					if (list2.indexOf(list1[i]) >= 0) {
						count++;
					}
				}
				return count == len1;
			}
			return true;
		}
	}
}